using System;
using System.Collections.Generic;
using System.Text;
using BizTalkCop.Rules.Bases;
using Microsoft.FxCop.Sdk;
using Microsoft.BizTalk.ExplorerOM;

namespace BizTalkCop.Rules.Naming.Rules
{
    /// <summary>
    /// Rule verifying send port groups are correctly prefixed
    /// </summary>
    public class SendPortGroupsShouldHaveCorrectPrefix : ApplicationRuleBase
    {
        /// <summary>
        /// Default constructor
        /// </summary>
        public SendPortGroupsShouldHaveCorrectPrefix() : base("SendPortGroupsShouldHaveCorrectPrefix") { }

        /// <summary>
        /// Checks if the send port group name is correctly prefixed
        /// </summary>
        /// <param name="application">Application to check</param>
        /// <returns>Problem collection</returns>
        protected override ProblemCollection Check(Application application)
        {
            foreach (SendPortGroup group in application.SendPortGroups)
            {
                string prefix = this.GetConfiguredSetting(Setting.Prefix);
                if (!group.Name.StartsWith(prefix))
                {
                    Problems.Add(new Problem(GetNamedResolution("Prefix", group.Name, prefix), application.Name));
                }
            }
            return Problems;
        }

        #region Configured settings

        static SendPortGroupsShouldHaveCorrectPrefix()
        {
            Dictionary<string, string> defaults = new Dictionary<string, string>();
            defaults.Add(Setting.Prefix, Default.Prefix);
            SetDefaults(typeof(SendPortGroupsShouldHaveCorrectPrefix), defaults);
        }

        private struct Setting
        {
            public const string Prefix = "Prefix";
        }

        private struct Default
        {
            public const string Prefix = "SendPortGroup.";
        }

        #endregion
    }
}
